package edu.ustb.efws.pipeline.collect.handler;

import edu.ustb.efws.EfwsProperties;
import edu.ustb.efws.base.domain.Position;
import edu.ustb.efws.base.service.PositionService;
import edu.ustb.efws.base.service.SmsService;
import edu.ustb.efws.base.service.UserService;
import edu.ustb.efws.monitor.service.DetectorMonitorService;
import edu.ustb.efws.pipeline.collect.Collect;
import edu.ustb.efws.pipeline.collect.cache.MaxICacheService;
import edu.ustb.efws.pipeline.collect.handler.meter.MeterPointPool;
import edu.ustb.efws.pipeline.service.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现{@link PipelineHandler}处理
 *
 * @author TS Group
 */
public class CollectChina {
    private final DetectorMonitorService detectorMonitorService;
    private final PipelineChain iptChain;
    private final PipelineChain thosChain;
    private final PipelineChain kyjChain;

    /**
     * 构造{@link CollectChina}
     *
     * @param iptLogService {@link IptLogService}
     * @param thosLogService {@link ThosLogService}
     * @param positionService {@link PositionService}
     * @param riskLogService {@link RiskLogService}
     * @param pool {@link MeterPointPool}
     * @param properties {@link EfwsProperties}
     * @param userService {@link UserService}
     * @param smsService {@link SmsService}
     * @param detectorMonitorService {@link DetectorMonitorService}
     * @param maxICacheService {@link MaxICacheService}
     */
    public CollectChina(IptLogService iptLogService, ThosLogService thosLogService,
                        KyjLogService kyjLogService, PositionService positionService,
                        RiskLogService riskLogService, MeterPointPool pool,
                        EfwsProperties properties, UserService userService,
                        SmsService smsService, DetectorMonitorService detectorMonitorService,
                        MaxICacheService maxICacheService){

        this.detectorMonitorService = detectorMonitorService;

        List<PipelineHandler> iptHandlers = new ArrayList<>(3);
        iptHandlers.add(new IptLogHandler(iptLogService, maxICacheService, properties));
        List<Position> positions = positionService.all();
        iptHandlers.add(new IptRiskLogHandler(positions, riskLogService, maxICacheService, detectorMonitorService));
        iptHandlers.add(new MeterPointHandler(pool));
        iptHandlers.add(new WarSmsHandler(properties, userService, smsService, positionService));
        this.iptChain = new PipelineChain(iptHandlers);

        List<PipelineHandler> thosHandlers = new ArrayList<>(3);
        thosHandlers.add(new ThosLogHandler(thosLogService, properties));
        thosHandlers.add(new ThosRiskLogHandler(positions, riskLogService));
        thosHandlers.add(new MeterPointHandler(pool));
        thosHandlers.add(new WarSmsHandler(properties, userService, smsService, positionService));
        this.thosChain = new PipelineChain(thosHandlers);

        List<PipelineHandler> kyjHandlers = new ArrayList<>(3);
        kyjHandlers.add(new KyjLogHandler(kyjLogService, maxICacheService, properties));
        kyjHandlers.add(new KyjRiskLogHandler(positions, riskLogService, maxICacheService, detectorMonitorService));
        kyjHandlers.add(new MeterPointHandler(pool));
        kyjHandlers.add(new WarSmsHandler(properties, userService, smsService, positionService));
        this.kyjChain = new PipelineChain(kyjHandlers);
    }

    public void doHandle(Collect collect) {

        detectorMonitorService.active(collect.getDetId());

        if(collect.getType() == Collect.Type.IPT){
            iptChain.clone().doHandle(collect);
        }

        if(collect.getType() == Collect.Type.THOS){
            thosChain.clone().doHandle(collect);
        }

        if(collect.getType() == Collect.Type.KYJ){
            kyjChain.clone().doHandle(collect);
        }
    }
}
